package com.csaba.db.wizards;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.dialogs.IDialogPage;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import com.csaba.db.connection.PasswordComplexity;

/**
 * The "New" wizard page allows setting the container for the new file as well
 * as the file name. The page will only accept file name without the extension
 * OR with the extension that matches the expected one (mpe).
 */

public class NewDBWizzardPage extends WizardPage
{
	private Text userName;

	private Text password;

	private Text passwordConfirm;

	/**
	 * Constructor for SampleNewWizardPage.
	 * 
	 * @param pageName
	 */
	public NewDBWizzardPage()
	{
		super("wizardPage");
		setTitle("New Database Wizard");
		setDescription("This wizard creates a new encrypted database for storing sensitive information, protected by a password.");
	}

	/**
	 * @see IDialogPage#createControl(Composite)
	 */
	public void createControl( final Composite parent )
	{
		final Composite container = new Composite(parent, SWT.NULL);
		final GridLayout layout = new GridLayout();
		container.setLayout(layout);
		layout.numColumns = 2;
		layout.verticalSpacing = 9;

		new Label(container, SWT.NULL).setText("&Database name (db project name):");

		userName = new Text(container, SWT.BORDER | SWT.SINGLE);
		userName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		userName.addModifyListener(new ModifyListener()
		{
			public void modifyText( final ModifyEvent e )
			{
				dialogChanged();
			}
		});

		new Label(container, SWT.NULL).setText("&Password:");

		password = new Text(container, SWT.BORDER | SWT.SINGLE);
		password.setEchoChar('*');
		password.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		password.addModifyListener(new ModifyListener()
		{
			public void modifyText( final ModifyEvent e )
			{
				dialogChanged();
			}
		});

		new Label(container, SWT.NULL).setText("&Confirm password:");

		passwordConfirm = new Text(container, SWT.BORDER | SWT.SINGLE);
		passwordConfirm.setEchoChar('*');
		passwordConfirm.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		passwordConfirm.addModifyListener(new ModifyListener()
		{
			public void modifyText( final ModifyEvent e )
			{
				dialogChanged();
			}
		});

		dialogChanged();
		setControl(container);
	}

	/**
	 * Ensures that both text fields are set.
	 */
	private void dialogChanged()
	{
		final String userName = getUserName();
		final String password = getPassword();
		final String passwordConfirm = getPasswordConfirm();

		if ( !PasswordComplexity.checkPassword(password) )
		{
			updateStatus("The password is empty or not enough complex.");
			return;
		}
		if ( !password.equals(passwordConfirm) )
		{
			updateStatus("The 2 passwords do not match.");
			return;
		}

		if ( userName == null || userName.length() < 1 )
		{
			updateStatus("Database name must be specified.");
			return;
		}
		if ( userName.replace('\\', '/').indexOf('/', 1) > 0 )
		{
			updateStatus("Database name must be valid.");
			return;
		}

		//check for existing users
		final IResource userProject = ResourcesPlugin.getWorkspace().getRoot().findMember(new Path(userName));
		if ( userProject != null && userProject.exists() )
		{
			updateStatus("Database already exists.");
			return;
		}

		updateStatus(null);
	}

	public String getUserName()
	{
		return userName.getText();
	}

	public String getPassword()
	{
		return password.getText();
	}

	public String getPasswordConfirm()
	{
		return passwordConfirm.getText();
	}

	private void updateStatus( final String message )
	{
		setErrorMessage(message);
		setPageComplete(message == null);
	}

}